package com.bitoon.basketdudes.web;

import com.bitoon.basketdudes.web.jpa.Semanal;
import java.util.List;
import javax.persistence.*;
import org.apache.log4j.Logger;

public class WeeklyBoard extends BaseAction {

    // Parámetros entrada
    private String weeklyUrl;
    private Integer division;

    // Parámetros salida
    private List<Semanal> weeklyBoard;
    private String nombreTorneo;

    private Logger log = Logger.getLogger(WeeklyBoard.class);

    public String execute() {
        log.debug("Buscando weekly " + weeklyUrl + ", division " + division);

        EntityManager em = null;

        try {
            em = emfBasket.createEntityManager(); // checked

            Query q = em.createNamedQuery("Semanal.findByTorneoDivision");
            q.setParameter("urlTorneo", weeklyUrl);
            q.setParameter("division", division);

            List<Semanal> s = (List<Semanal>)q.getResultList();
            setWeeklyBoard(s);
            setNombreTorneo(s.get(0).getTorneo().getNombre());

        } catch (Exception e) {
            log.error("[execute]: Error: " + e);
        } finally {
            try { em.close(); } catch (Exception e) { }
        }

        return SUCCESS;
    }

    public String getWeeklyUrl() {
        return weeklyUrl;
    }

    public void setWeeklyUrl(String weeklyUrl) {
        this.weeklyUrl = weeklyUrl;
    }

    public Integer getDivision() {
        return division;
    }

    public void setDivision(Integer division) {
        this.division = division;
    }

    public List<Semanal> getWeeklyBoard() {
        return weeklyBoard;
    }

    public void setWeeklyBoard(List<Semanal> weeklyBoard) {
        this.weeklyBoard = weeklyBoard;
    }

    public String getNombreTorneo() {
        return nombreTorneo;
    }

    public void setNombreTorneo(String nombreTorneo) {
        this.nombreTorneo = nombreTorneo;
    }
    
}
